<?php 
/**
 * @version        $Id$
 * @author         master@xuewl.com
 * @copyright      Copyright (c) 2007 - 2014, Chongqing xuewl Information Technology Co., Ltd.
 * @link           http://www.xuewl.com
**/
namespace Member\Controller;
use \Common\Controller\BaseController;
if (!defined('MODULE_CACHE')) define('MODULE_CACHE', DATA_PATH.'caches_model/');
class IndexController extends BaseController {
	public function _initialize() {
		parent::_initialize();
		$this->db = D('Member');
	}

	public function index() {
		redirect(U('Profile/index'));
	}

	public function mini() {		
	}
	/**
	 * 会员注册
	 * @author xuewl <master@xuewl.com>
	 */
	public function register() {
		$models = getcache('model', 'commons');
		$modelid = I('modelid', '1', 'intval');
		$setting = getcache('setting', 'member');
		if (IS_POST) {
			$info = I('post.');

			$info['modelid'] = 1;
			/*$city = $this->getAreaName($info['province']);
			$district = $this->getAreaName($info['district']);*/
			$data['city'] = $info['province'];
			$data['district'] = $info['district'];
			$data['address'] = $info['address'];

			$info['address'] =array2string($data);
			$info['code'] = $info['code'];
			$MemberLogic = D('Member', 'Logic');
			$result = $MemberLogic->register($info);

			if (!$result) {
				$this->error($MemberLogic->getError());
			}
			cookie('_userid', $result);
			cookie('_groupid', 3);
			
			$this->db->where(array('userid'=>$result))->setField('status', 1);
			$this->success('注册成功', U('Document/Index/index'));
		} else {
			require MODULE_CACHE.'member_form.class.php';
			$member_form = new \member_form($modelid);
			$forminfos = $member_form->get();
			$region = D('Linkage')->where(array('parentid'=>0))->select();//获取地址列表
			$SEO=seo(0,"用户注册");
			include template('register');
		}
	}


	    /**
     * ajax获取地区
     *@author wj 
     *@date  2014-10-14
     */
    public function getArea() {
        $where['parentid'] = $_REQUEST['areaId'];
        $area = D('Linkage')->where($where)->select();
        echo json_encode($area);
        
    }

 

	/**
	 * 会员登陆
	 * @author xuewl <master@xuewl.com>
	 */
	public function login() {
		if (IS_POST) {
			$info = I('post.');
			$MemberLogic = D('Member', 'Logic');
			$result = $MemberLogic->login($info);
			if (!$result) {
				$this->error($MemberLogic->getError());
			}
			$this->success('登录成功', U('Document/Index/index'));
		} else {
			$SEO=seo(0,"用户登录");
			include template('login');
		}		
	}

	/**
	 * 会员登出
	 * @author xuewl <master@xuewl.com>
	 */
	public function logout() {
		cookie('_userid', NULL);
		cookie('_groupid', 8);
		$this->success('操作成功', U('login'));
	}

	/**
	 * 邮件认证
	 * @author xuewl <master@xuewl.com>
	 */
	public function verify_email() {
		$code = I('code');
		if (empty($code)) {
			$this->error('操作失败');
		}
		$string = authcode($code, 'DECODE');
		list($uid, $code) = explode("|", $string);
		$uid = (int) $uid;
		if ($uid < 1 || $code != md5(C('AUTHKEY'))) {
			$this->error('认证编码错误');
		}
		$this->db->where(array('userid' => $uid))->setField('email_status', 1);
		$this->success('您的邮箱认证成功', U('login'));
	}

	/**
	 * 找回密码
	 * @author xuewl <master@xuewl.com>
	 */
	public function forget() {
		if (IS_POST) {
			$info = I('post.');
			if($info['type'] == 'email') {
				$email = trim(remove_xss($info['field']));
				if (empty($email)) {
					$this->error('邮箱不能为空');
				}
				if (!isemail($email)) {
					$this->error('邮箱格式不正确');
				}
				$result = $this->db->where(array('email' => $email))->find();
				if (!$result) {
					$this->error('没有找到此用户');
				}
				helpers('mail');
				sendmail($result['email'], '密码找回邮件', '您的新密码为');
				cookie('forget_email', $result['email']);
				$this->success('操作成功', U('email_success'));
			} else {
				$phone = trim(remove_xss($info['field']));
				if (empty($phone)) {
					$this->error('手机号码不能为空');
				}
				if (!is_mobile($phone)) {
					$this->error('手机号码不正确');
				}
				$Verify = new \Think\Verify();
				if ($Verify->check($info['verify']) === FALSE) {
					$this->error('验证码输入错误');
				}
				$result = $this->db->where(array('phone' => $phone))->find();
				if (!$result) {
					$this->error('没有找到此用户');
				}

				$_vcode = random(6, 1);
				$info = array ('send_userid' => $this->userid,'mobile' => $phone,'msg'=> $_vcode);
				$this->sms_db = D('Sms/SmsReport');
				$result_id = $this->sms_db->update($info);
				if (!$result_id) {
					$this->error($this->sms_db->getError());
				}
				$SmsApi = new \Sms\Api\SmsApi();
				$sms_result = $SmsApi->send($phone, '您的校验码为[ '.$_vcode.' ]，请尽快验证。');
				if (!$sms_result) {
					$this->sms_db->where(array('id' => $result_id))->delete();
					$this->error($SmsApi->getError());
				}
				$this->success('操作成功', U('phone_verify'));
			}
		} else {			
			include template('forget/forget');
		}
	}

	/**
	 * 手机短信校验
	 * @author xuewl <master@xuewl.com>
	 */
	public function phone_verify($verify = '') {
		if (IS_POST) {
			$verify = dhtmlspecialchars(addslashes($verify));
			if (empty($verify)) {
				$this->error('验证码不能为空');
			}
			$result = D('Sms/SmsReport')->where(array('userid' => $this->userid, 'msg' => $verify, 'status' => '0'))->order('id DESC')->find();
			if (!$result) {
				$this->error('验证码错误或已过期');
			}
			session('_forget_userid_', $result['send_userid']);
			D('Sms/SmsReport')->where(array('id' => $result['id']))->setField('status', '1');
			$this->success('验证成功，请设置新密码', U('resetpwd'));
		} else {
			include template('forget/phone_verify');
		}
	}

	/**
	 * 重新设置密码
	 * @author xuewl <master@xuewl.com>
	 */
	public function resetpwd() {
		$userid = session('_forget_userid_');
		$userinfo = $this->db->getByUserid($userid);
		if (IS_POST) {
			$info = I('post.');
			if (empty($info['password'])) {
				$this->error('新密码不能为空');
			}
			if ($info['password'] != $info['2password']) {
				$this->error('两次密码不一致！！');
			}
			$password = md5(md5($info['password'].$userinfo['encrypt']));
			$result = $this->db->where(array('userid' => $userid))->setField('password', $password);
			if (!$result) {
				$this->error('密码修改失败');
			} else {
				session('_forget_userid_', NULL);
				$this->success('操作成功', U('phone_success'));
			}
		} else {
			include template('forget/resetpwd');
		}		
	}

	public function phone_success() {
		include template('forget/phone_success');
	}

	public function email_success() {
		$email = cookie('forget_email');
		if ($email) {
			cookie('forget_email', NULL);
			include template('forget/email_success');
		} else {
			$this->error('请勿非法访问');
		}
	}


}